﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace Kinect_Interface_Prototype_1
{
    public partial class ConfirmationWindow : Form
    {
        int counter;
        private const int GWL_STYLE = -16;
        private const int WS_SYSMENU = 0x80000;
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

        public string command = "";

        private StartupProgram MainProgram;

        //** Important**
        // Only used in StartupProgram, use method in start up program to obtain otherwise
        public ConfirmationWindow(StartupProgram program)
        {
            InitializeComponent();
            MainProgram = program;
            YesButton.Visible = true;
            NoButton.Visible = true;
            button1.Visible = false;
            counter = 6;
            timer.Tick += new EventHandler(timer_Tick);// Start the timer
        }

        public void NoButton_Click(object sender, EventArgs e)
        {
            abortLabel.Text = "";
            this.Hide();
        }

        public void YesButton_Click(object sender, EventArgs e)
        {
            // skip double checking (timer and abort) when user clicks on the close button
            if (command == "exit")
            {
                this.Hide();
                MainProgram.ShutDown();
            }

            abortLabel.Text = "";
            button1.Visible = true;
            YesButton.Visible = false;
            NoButton.Visible = false;
            counter = 6;
            MainProgram.VoiceCommands_ToggleActive(false);
            MainProgram.ControlForm_ToggleVoiceCommandsSwitch(false);
            timer.Interval = (1000) * (1);              
            timer.Enabled = true;                       
            timer.Start();
         
        }

        private void performCommand()
        {
            switch (command)
            {
                case "system shutdown":
                    MainProgram.ShutDown();
                    break;
                case "logoff":
                    System.Diagnostics.Process.Start("ShutDown", "-l");
                    break;
                case "shutdown":
                     System.Diagnostics.Process.Start("ShutDown", "-s");
                    break;
                case "restart":
                    System.Diagnostics.Process.Start("ShutDown", "-r");
                    break;
                case "Hibernate":
                    System.Diagnostics.Process.Start("ShutDown", "-h");
                    break;
                default:
                    break;
            } 
        }

        private void ConformationWindow_Load(object sender, EventArgs e)
        {
            var hwnd = this.Handle;
            SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
        }

        private void ConformationWindow_Shown(object sender, EventArgs e)
        {
            // Center label
            textLabel.Location = new Point(this.Width / 2 - textLabel.Width / 2, textLabel.Location.Y);
            counter = 6;
            abortLabel.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer.Stop();
            this.Hide();
            YesButton.Visible = true;
            NoButton.Visible = true;
            button1.Visible = false;
            counter = 6;
            MainProgram.VoiceCommands_ToggleActive(true);
            MainProgram.ControlForm_ToggleVoiceCommandsSwitch(true);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            counter--;
            abortLabel.Text = counter.ToString();

            if (counter == 0)
            {
                timer.Stop();
                performCommand();
            }
        }
    }
}
